<?php

/**
 * 共享函数类
 * @copyright (c) 2013, Mr.Wang
 * @author Mr.Wang <281762391@qq.com>
 */
class ShareFunc {

    public static function getEchoJson($message, $nextURL) {
        $arr = array(
            'message' => $message,
            'nextURL' => $nextURL
        );

        return json_encode($arr);
    }

    public static function setPageTipInfo($message, $status = 'success', $link_name = '', $link = '') {
        $info['message']    = $message;
        $info['status']     = $status;
        $info['buttonName'] = $link_name;
        $info['function']   = $link;

        $_SESSION['tipsInfo'] = $info;

        //Log::write($message, Log::ERR);
    }

    public static function setPreviousLoginInfo($url, $param) {
        $_SESSION['preURL']   = $url;
        $_SESSION['preParam'] = $param;
    }

    public static function mkdirs($dir, $mode = 0777) {
        if (is_dir($dir) || @mkdir($dir, $mode))
            return TRUE;

        if (!ShareFunc::mkdirs(dirname($dir), $mode))
            return FALSE;

        return @mkdir($dir, $mode);
    }

    public static function trimUtf8String($string, $lengthMax) {
        if (mb_strlen($string, 'utf-8') > $lengthMax) {
            $string = mb_substr($string, 0, $lengthMax - 3, 'utf-8') . '...';
        }
        return $string;
    }

    public static function rehtml($content) {
        $content = trim($content);
        $content = strip_tags($content, "");
        $content = ereg_replace("\t", " ", $content);
        $content = ereg_replace("\r\n", "  ", $content);
        $content = ereg_replace("\r", "", $content);
        $content = ereg_replace("\n", "  ", $content);
        $content = ereg_replace("\t", "  ", $content);
        $content = str_replace(" ", " ", $content);
        return trim($content);
    }

    public static function array_sort($arr, $keys, $type = 'asc') {
        $keysvalue = $new_array = array();
        foreach ($arr as $k => $v) {
            $keysvalue[$k] = $v[$keys];
        }
        if ($type == 'asc') {
            asort($keysvalue);
        } else {
            arsort($keysvalue);
        }
        reset($keysvalue);

        foreach ($keysvalue as $k => $v) {
            $new_array[$k] = $arr[$k];
        }
        return $new_array;
    }

    public static function getPictureDirectory($itemID) {
        $l1 = intval($itemID / (250 * 800 * 800));
        $l2 = intval($itemID / (250 * 800));
        $l3 = intval($itemID / (250));

        return $l1 . '/' . $l2 . '/' . $l3 . '/';
    }

    public static function timeTransfer($time, $showSecond = false) {
        $now_time = time();
        $dur      = $now_time - $time;
        if ($dur < 3) {
            return 3 . '秒前';
        }
        if ($dur < 60) {
            return $dur . '秒前';
        } else {
            if ($dur < 3600) {
                return floor($dur / 60) . '分钟前';
            } else {
                if ($dur < 86400) {
                    return floor($dur / 3600) . '小时前';
                } else {
                    if ($dur < 259200) { //3天内
                        return floor($dur / 86400) . '天前';
                    } else {
                        //返回 '3天前';
                        if (!$showSecond) {
                            return date("Y-m-d", $time);
                        } else {
                            return date("Y-m-d, H:i:s", $time);
                        }
                    }
                }
            }
        }
    }

}

?>